<?php

namespace component\element\checkbox\controller;

use app\bll\Datadict as BllDatadict;
use core\component\ElementBase;

class Index extends ElementBase
{
    public function index(...$param)
    {
        $datasource   = [];
        $displayvalue = 'displayname';
        $realvalue    = 'realvalue';
        switch ($param['sourcetype']) {
            case 1:
                $blDatadict   = new BllDatadict();
                $dict         = $blDatadict->find($param['sourceval']);
                $datasource   = $dict['values'];
                break;
            case 2:
                $sourceval = explode("\n", str_replace("\r\n", "\n", $param['sourceval']));

                foreach ($sourceval as $key => $val) {
                    list($cusrealvalue, $cusdisplayname) = explode('|', $val);
                    $datasource[] = [
                        'displayname' => $cusdisplayname,
                        'realvalue'   => $cusrealvalue
                    ];
                }
                break;
        }
        
        return $this->view(array_merge($param, [
            'datasource'   => $datasource,
            'displayvalue' => $displayvalue,
            'realvalue'    => $realvalue,
        ]));
    }
}
